//
//  CollectionViewCell.m
//  gdemm
//
//  Created by 乔明超 on 2017/8/8.
//  Copyright © 2017年 Goldencis. All rights reserved.
//

#import "CollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import <objc/runtime.h>
#import "Utils.h"
#import "getAppInfo.h"
#import "UIView+Extension.h"
@interface CollectionViewCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *appName;
@property (weak, nonatomic) IBOutlet UILabel *appSize;
@property (weak, nonatomic) IBOutlet UIView *iconView;
@property (weak, nonatomic) IBOutlet UIButton *appState;
//@property (strong, nonatomic) UIButton *progressBtn;
@end
@implementation CollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.appState.backgroundColor =[Utils colorWithKey:@"mainBgColor"];
}

//-(UIButton *)progressBtn{
//    if (_progressBtn == nil) {
//        _progressBtn = [[UIButton alloc]init];
//
//        _progressBtn.backgroundColor = [Utils UIColorFromHex:0xe4e4e4];
////        [_progressBtn setTitleColor:[Utils UIColorFromHex:0xffffff] forState:UIControlStateNormal];
////        _progressBtn.titleLabel.font = [UIFont systemFontOfSize:15];
////        [_progressBtn setTitle:@"安装中" forState:UIControlStateNormal];
//        _progressBtn.frame = CGRectMake(0, 0, 75, 30);
//        [_appState addSubview:_progressBtn];
//    }
//    return _progressBtn;
//}

-(void)setAppInfo:(AppInfoModel *)appInfo{
     _appInfo = appInfo;
    NSString *servAddr = [[UserDefaultsUtil shareInstance]getString:LC_SERVADDR];
    if(![StringUtil isEmpty:servAddr]){
   NSString * iconPathStr = [NSString stringWithFormat:@"http://%@%@",servAddr,appInfo.iconPath];
    NSURL * url = [NSURL URLWithString:iconPathStr];
    [self.iconImage  sd_setImageWithURL:url];
    }
    self.appName.text = appInfo.name;
    [self.appState setTitle:appInfo.appState forState:UIControlStateNormal];
    self.appSize.text = appInfo.size;
    if ([appInfo.appState  isEqualToString:@"打开"]) {
        self.appState.backgroundColor = [Utils UIColorFromHex:0xffffff];
        self.appState.layer.borderColor = [Utils colorWithKey:@"mainBgColor"].CGColor;//[Utils UIColorFromHex:0x26365a].CGColor;
        self.appState.layer.borderWidth = 3;
        //[self.appState setTitleColor:HRColor(43, 52, 93) forState:UIControlStateNormal];
        [self.appState setTitleColor:[Utils colorWithKey:@"mainBgColor"] forState:UIControlStateNormal];
        
    }else if ([appInfo.appState  isEqualToString:@"安装"]){
        self.appState.layer.borderWidth = 0;
        self.appState.backgroundColor = [Utils colorWithKey:@"mainBgColor"];//[Utils UIColorFromHex:0x26365a];
        [self.appState setTitleColor:[Utils UIColorFromHex:0xffffff] forState:UIControlStateNormal];
        
    }else if ([appInfo.appState  isEqualToString:@"更新"]){
        self.appState.layer.borderWidth = 0;
        self.appState.backgroundColor = [Utils UIColorFromHex:0x0ea67d];
        [self.appState setTitleColor:[Utils UIColorFromHex:0xffffff] forState:UIControlStateNormal];
    }else{
        self.appState.layer.borderWidth = 0;
        self.appState.backgroundColor = [Utils colorWithKey:@"mainBgColor"];//[Utils UIColorFromHex:0x26365a];
        [self.appState setTitleColor:[Utils UIColorFromHex:0xffffff] forState:UIControlStateNormal];
    }

}
- (IBAction)appStateBtnClick:(id)sender {
    if ([self.appInfo.appState  isEqualToString:@"打开"]) {
        Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
        NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
        //通过Bundle打开app
        [workspace performSelector:NSSelectorFromString(@"openApplicationWithBundleID:") withObject:self.appInfo.packageName];
    }
    if([self.appInfo.appState  isEqualToString:@"安装"] || [self.appInfo.appState  isEqualToString:@"更新"]){
        NSString *servAddr = [[UserDefaultsUtil shareInstance]getString:LC_SERVADDR];
        NSString * url = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https://%@/Resource/%@",servAddr,self.appInfo.plistPath];
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
    }
}


- (void)layoutSubviews{
    [super layoutSubviews];
    self.appState.layer.cornerRadius = self.appState.height/2;
    self.iconImage.layer.cornerRadius = 15;
    self.iconImage.layer.masksToBounds = YES;
    
   }
@end
